// subpkg_fask/pages/carBack/carBack.js
Page({
  data: {
    radio: '2', //是否违章
    radioOne: '2', //是否故障
    radioTwo: '2', //是否事故
    isBreakRules: false, // 违章信息是否显示
    isFault: false, //故障信息是否显示
    isAccident: false, //事故信息是否显示
    startTime: '2022.05.04 13:00', //出车时间
    endTime: '2022.05.05 13:00', //回车时间
    popTime: false,
    breakRulesTypeList: [{
      id: 1,
      name: '闯红灯'
    }, {
      id: 2,
      name: '无证驾驶'
    }, {
      id: 3,
      name: '超载'
    }, {
      id: 4,
      name: '酒后驾驶'
    }, {
      id: 5,
      name: '超速行驶'
    }, {
      id: 6,
      name: '其他'
    }], //违章类型列表
    breakRulesType: 1, //违章类型
    breakRulesDescription: '', //违章说明，类型为“其他”时填写
    penaltyAmount: 0, //罚款金额
    deductPoints: 0, //扣分数据
    faultType: 1, //故障类型
    faultTypeList: [{
        id: 1,
        name: '发动机启动困难'
      },
      {
        id: 2,
        name: '不着车'
      },
      {
        id: 3,
        name: '漏油'
      },
      {
        id: 4,
        name: '漏水'
      },
      {
        id: 5,
        name: '照明失灵'
      },
      {
        id: 6,
        name: '有异响'
      },
      {
        id: 7,
        name: '排烟异常'
      },
      {
        id: 8,
        name: '温度异常'
      },
      {
        id: 9,
        name: '其他'
      }
    ], //故障列表
    faultDescription: '', //故障说明，类型为“其他”时填写
    deductPointsList: [0, 1, 2, 3, 6, 12], //扣分列表
    penaltyAmountList: [0, 100, 200, 300, 500, 1000, 2000], //罚款金额列表
    accidentType: 1, //事故类型
    accidentTypeList: [{
        id: 1,
        name: '直行事故'
      },
      {
        id: 2,
        name: '追尾事故'
      },
      {
        id: 3,
        name: '超车事故'
      },
      {
        id: 4,
        name: '左转弯事故'
      },
      {
        id: 5,
        name: '右转弯事故'
      },
      {
        id: 6,
        name: '弯道事故'
      },
      {
        id: 7,
        name: '坡道事故'
      },
      {
        id: 8,
        name: '会车事故'
      },
      {
        id: 9,
        name: '其他'
      }
    ], //事故类型列表
    accidentDescription:'',//事故说明，类型为“其他”时填写
    faultImagesList:[],//故障照片
    accidentImagesList:[],//事故照片
    currentDate: new Date(),
    minDate: new Date().getTime(),
    maxDate: new Date(2023, 11, 31).getTime(),
    formatter(type, value) {
      if (type === 'month') {
        return `${value}月`;
      } else if (type === 'day') {
        return `${value}日`;
      } else if (type === 'hour') {
        return `${value}点`;
      } else if (type === 'minute') {
        return `${value}分`;
      }
      return value;
    },
  },
  onLoad(options) {
    const timestamp = this.timestamp(Date.now())
    this.setData({
      endTime: timestamp
    })
  },
  vanColOne(e) { //改变违章类型
    // console.log(e.mark.id);
    this.setData({
      breakRulesType: e.mark.id
    })
  },
  vanColTwo(e) { //改变罚款金额
    // console.log(e.mark.id);
    this.setData({
      penaltyAmount: e.mark.id
    })
  },
  afterRead(e){//故障照片临时生成
    // console.log(e);
    console.log(e.detail.file.url);
    const url = e.detail.file.url
    this.setData({faultImagesList:[...this.data.faultImagesList,url]})
  },
  delete(e){  //删除故障照片
    // console.log(e);
    const index = e.detail.index
    const faultImagesList = this.data.faultImagesList.splice(index,1)
    this.setData({faultImagesList})
  },
  afterReadOne(e){ //事故照片
  console.log(e);
  console.log(e.detail.file.url);
  const url = e.detail.file.url
  this.setData({accidentImagesList:[...this.data.accidentImagesList,url]})
  // console.log(this.data.accidentImagesList);
  },
  deleteOne(e){//删除事故照片
    // console.log(e);
    const index = e.detail.index
    const accidentImagesList = this.data.accidentImagesList.splice(index,1)
    this.setData({accidentImagesList})
  },
  vanColThree(e) { //改变扣分
    // console.log(e.mark.id);
    this.setData({
      deductPoints: e.mark.id
    })
  },
  vanColFour(e) { //改变故障类型
    // console.log(e.mark.id);
    this.setData({
      faultType: e.mark.id
    })
  },
  vanColFive(e) {
    // console.log(e.mark.id);
    this.setData({
      accidentType: e.mark.id
    })
  },
  onClickLeft() {
    console.log('返回');
    wx.navigateBack({
      delta: 1 // 返回页面的层数，默认为1，即返回上一个页面
    })
  },
  returnTime() { //弹出时间选择
    this.setData({
      popTime: true
    })
  },
  onChange(event) {
    if (event.detail === '1') {
      this.setData({
        radio: '1',
        isBreakRules: true // 显示违章信息
      })
    } else {
      this.setData({
        radio: '2',
        isBreakRules: false // 隐藏违章信息
      })
    }
  },
  onChangeOne(event) {
    if (event.detail === '1') {
      this.setData({
        radioOne: '1',
        isFault: true // 显示故障信息
      })
    } else {
      this.setData({
        radioOne: '2',
        isFault: false // 隐藏故障信息
      })
    }
  },
  onChangeTwo(event) {
    if (event.detail === '1') {
      this.setData({
        radioTwo: '1',
        isAccident: true // 显示车辆信息
      })
    } else {
      this.setData({
        radioTwo: '2',
        isAccident: false // 隐藏车辆信息
      })
    }
  },
  cancel() { //取消时间按钮
    this.setData({
      popTime: false
    })
  },
  confirm(e) {
    console.log('时间戳', e.detail);
    const timestamp = this.timestamp(e.detail)
    console.log('确认', timestamp);
    this.setData({
      endTime: timestamp
    })
    this.cancel()
  },
  timestamp(timestamp) {
    const date = new Date(timestamp);
    const year = date.getFullYear();
    const month = String(date.getMonth() + 1).padStart(2, '0');
    const day = String(date.getDate()).padStart(2, '0');
    const hour = String(date.getHours()).padStart(2, '0');
    const minute = String(date.getMinutes()).padStart(2, '0');
    const formattedDate = `${year}.${month}.${day} ${hour}:${minute}`;
    return formattedDate
  },
  async submitCar() {
    const {
      accidentDescription,
      accidentImagesList,
      accidentType,
      breakRulesDescription,
      breakRulesType,
      deductPoints,
      endTime,
      faultDescription,
      faultImagesList,
      faultType,
      id,
      isAccident,
      isAvailable,
      isBreakRules,
      isFault,
      penaltyAmount,
      startTime
    } = this.data
    const res = await wx.http.post('/driver/tasks/truckRegistration', {
      accidentDescription,
      accidentImagesList,
      accidentType,
      breakRulesDescription,
      breakRulesType,
      deductPoints,
      endTime,
      faultDescription,
      faultImagesList,
      faultType,
      id,
      isAccident,
      isAvailable,
      isBreakRules,
      isFault,
      penaltyAmount,
      startTime
    })
    wx.$toast('交车成功!')
    console.log(res);
  }

})